import {
  Body,
  Controller,
  Get,
  Logger,
  Param,
  Patch,
  Post,
} from '@nestjs/common';
import { BaseController } from '@/controllers/base.controller';
import { halt, ok } from '@/utils';
import { ClockinService } from '@/services/clockin.service';
import { UserService } from '@/services/user.service';
import { CurrentUser } from '@/decorators';
import dayjs from 'dayjs';

@Controller('clockin')
export class ClockinController extends BaseController {
  constructor(
    private readonly ClockinService: ClockinService,
    private readonly UserService: UserService,
  ) {
    super();
  }

  @Get('index/:uuid')
  async index(@Param('uuid') uuid: string, @CurrentUser('id') uid: number) {
    const user = await this.UserService.findOne({
      id: uid,
    });
    if (!user) {
      return halt('用户数据异常');
    }
    return ok(
      await this.ClockinService.index(uuid, uid, user.code, user.isReal),
    );
  }

  @Post('index/:uuid')
  async create(@Param('uuid') uuid: string, @CurrentUser('id') uid: number) {
    if (!uuid) {
      return halt('参数异常');
    }
    const currentTime = dayjs();
    if (currentTime.isBefore(dayjs('2024-10-01'))) {
      return halt('活动尚未开始');
    }
    if (currentTime.isAfter(dayjs('2024-10-07'))) {
      return halt('活动已结束');
    }
    return ok(await this.ClockinService.create(uuid, uid));
  }
}
